const Koa = require('koa')
const path = require('path')
const bodyParser = require('./middleware/koa-bodyparser');
const static = require('./middleware/koa-static')
const Router = require('./middleware/koa-router')
const router = new Router()


const app = new Koa();
app.use(router.routes())
app.use(bodyParser())
app.use(static(path.resolve(__dirname, 'public')))
app.use(static(path.resolve(__dirname, 'koa')))

// app.use(async (ctx, next) => {
//     if (ctx.method === 'GET' && ctx.path === '/form') {
//         ctx.body = `
//         <form action="/form" method="POST">
//             <input type="text" name="username">
//             <input type="text" name="password">
//             <button>提交</button>
//         </form>
//         `
//         console.log(ctx.response.body)
//     } else {
//         await next();
//     }
// })
// app.use(async (ctx, next) => {
//     // 如何解析请求体？
//     if (ctx.method === 'POST' && ctx.path == '/form') {
//         ctx.type = 'text/plain;charset=utf-8'
//         ctx.body = ctx.request.body
//     }
// })
router.get('/user/add', async (ctx, next) => {
    ctx.body = 'user/add';
})
router.get('/user/remove', async (ctx, next) => {
    ctx.body = 'user/remove';
    console.log(1)
})
router.get('/user/remove', async (ctx, next) => {
    console.log(2)
    ctx.body = 'user/remove2';
})


app.listen(3000, () => {
    console.log(`server start 3000`);
})